
import dao.GuichetDAO;
import entity.Guichet;
import entity.Guichet;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.jboss.weld.context.ejb.Ejb;
@ManagedBean(name = "guichetBean")
@SessionScoped
public class guichetBean {
    @Ejb
    private GuichetDAO userDAO = new GuichetDAO();
    private List<Guichet> users;
    private String hide = "";
    private Guichet user = new Guichet();
    private Guichet userNew = new Guichet();
    private Guichet userToDelete = new Guichet();
    private Guichet userToEdit = new Guichet();
    private String secondPassword;
    private String showit;
    private Date date;

    public guichetBean() {
        userDAO.openSession();
       
    }

    public Date getDate() {
        return new Date();
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Guichet getUserToDelete() {
        return userToDelete;
    }
    
      public void redirect() throws IOException { 
        if (user.getId() == null) {
            System.out.println("User is null");

            ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();

            extContext.redirect("login_1.xhtml?faces-redirect=true");

        }
    }

    public void setUserToDelete(Guichet userToDelete) {
        this.userToDelete = userToDelete;
    }

    public Guichet getUserToEdit() {
        return userToEdit;
    }

    public void setUserToEdit(Guichet userToEdit) {
        this.userToEdit = userToEdit;
    }

    public String getSecondPassword() {
        return secondPassword;
    }

    public void setSecondPassword(String secondPassword) {
        this.secondPassword = secondPassword;
    }

    public String getHide() {
        return hide;
    }

    public void setHide(String hide) {
        this.hide = hide;
    }

    public Guichet getUser() {
        return user;
    }

    public void setUser(Guichet user) {
        this.user = user;
    }

    public List<Guichet> getUsers() {
        return userDAO.findAll();
    }

    public void setUsers(List<Guichet> users) {
        this.users = users;
    }

    public Guichet getUserNew() {
        return userNew;
    }

    public void setUserNew(Guichet userNew) {
        this.userNew = userNew;
    }
 
    private String hideIt;

    public String getHideIt() {
        return hideIt;
    }

    public void setHideIt(String hideIt) {
        this.hideIt = hideIt;
    }
    
    

     public void delete(Guichet userToDelete) {

        userDAO.beginTransaction();
        userDAO.delete(userToDelete);
        userDAO.commit();
        users = userDAO.findAll();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Guichet supprimé avec succès ", ""));



        userToDelete = new Guichet();


    }

    public String hideme() {
        try {
            if (userNew.getUsername().isEmpty() || userNew.getPassword().isEmpty()) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "empty ", ""));

                return "";
            }
            return "dlg3.hide();";
        } catch (NullPointerException e) {
            return "dlg3.show();";
        }
    }

    public String connect() {

        List<Guichet> lu = userDAO.findByLoginAndPass(user.getUsername(), user.getPassword());
        System.out.println("guichet connected " + lu.size());
        if (lu.size() == 0) {
            return "";
        }
        user = lu.get(0);

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "User ajouté  avec succès ", ""));

        return "vente_1.xhtml?faces-redirect=true";
    }
 public void add() {
        boolean save = true;
        System.out.println("Begin add");
        String msg = "Champs obligatoire:";

        if (userNew.getUsername().isEmpty()) {

            save = false;
            msg += "Username";
        }
        if (userNew.getPassword().isEmpty()) {

            save = false;
            msg += ",Password";
        }
        
        if (userNew.getOperateur().isEmpty()) {

            save = false;
            msg += ",Operateur";
        }


        if (secondPassword.isEmpty()) {

            save = false;
            msg += ",Confirm password";
        }
        if (!secondPassword.equals(userNew.getPassword()) && !userNew.getPassword().isEmpty() && !userNew.getPassword().isEmpty()) {
            save = false;
            String msg2 = "Mot de passe non identique";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, msg2, ""));

        }
        if (!save) {
            if (!msg.equals("Champs obligatoire:")) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, ""));
            }
            hideIt= "";
        }
        userDAO.beginTransaction();
        userDAO.save(userNew);
        userDAO.commit();
        userNew = new Guichet();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Guichet ajouté  avec succès ", ""));
        hideIt="dlg3.hide();";
        System.out.println(hideIt);
    }
    public String logout() {

        return "login_1.xhtml?faces-redirect=true";
    }
}
